package com.ican.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.UIBean;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;

import com.ican.common.hibernate.Page;
import com.opensymphony.xwork2.util.ValueStack;

@StrutsTag(description = "my page UI", name = "pageUIBean", tldTagClass = "com.ican.common.PageUITag")
public class PageUIBean extends UIBean {

    public static final String TEMPLATE_NAME = "mypage";
    private String pageBean;
    private String action;
    private String paramsKey;

    public PageUIBean(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
        super(stack, request, response);
    }
    
    @StrutsTagAttribute(description = "set pageBean", type = "String")
    public void setPageBean(String pageBean) {
        this.pageBean = pageBean;
    }

    @StrutsTagAttribute(description = "set action", type = "String")
    public void setAction(String action) {
        this.action = action;
    }
    
    @StrutsTagAttribute(description = "set paramsKey", type = "String")
    public void setParamsKey(String paramsKey) {
        this.paramsKey = paramsKey;
    }
    
    @Override
    protected String getDefaultTemplate() {
        return TEMPLATE_NAME;
    }
    
    @SuppressWarnings("rawtypes")
    @Override
    protected void evaluateExtraParams() {
        super.evaluateExtraParams();
        if(pageBean == null || pageBean.trim().length() < 1){
            pageBean = "page";
        }
        Page page = (Page) this.findValue(pageBean);
        if(page == null || page.getTotalPages() < 2){
            return;
        }
        addParameter("pageBean", pageBean);
        addParameter("cssClass", cssClass);
        addParameter("action", request.getContextPath() + action);
        int pageNo = page.getPageNo();
        long totalPages = page.getTotalPages();
        addParameter("pageNo", pageNo);
        addParameter("pageSize", page.getPageSize());
        addParameter("totalPages", totalPages);
        addParameter("currentPage", pageNo);
        addParameter("anyPage", totalPages);
        if(pageNo - 1 > 0){
            addParameter("prevPage", pageNo - 1);
        }
        if(pageNo - 2 > 0){
            addParameter("prevPage2", pageNo - 2);
        }
        if(pageNo + 1 <= totalPages){
            addParameter("nextPage", pageNo + 1);
        }
        if(pageNo + 2 <= totalPages){
            addParameter("nextPage2", pageNo + 2);
        }
        if(paramsKey != null && paramsKey.length() > 0){
            Object o = null;
            StringBuffer sb = new StringBuffer();
            for(String s : paramsKey.split(",")){
                s = s.trim();
                o = this.findValue(s);
                if(o != null){
                    sb.append("<input type=\"hidden\" name=\"");
                    sb.append(s);
                    sb.append("\" value=\"");
                    sb.append(o.toString());
                    sb.append("\"/>");
                }
            }
            if(sb.length() > 0){
                addParameter("pageFormParamsHtml", sb.toString());
            }
        }
    }
}
